﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using CultuRAM.Filters;
using WebMatrix.WebData;

namespace CultuRAM.DAL
{
        [InitializeSimpleMembership]
        public class ValidateLogin: Controller
        {
            private readonly UnitOfWork _unitOfWork = new UnitOfWork();

            //METHOD: Get UserName of User authenticated
            public string ValLogin()
            {
                if (WebSecurity.IsAuthenticated)
                {
                    var membershipUser = Membership.GetUser(WebSecurity.CurrentUserName);
                    if (membershipUser != null)
                    {
                        var user = membershipUser.UserName;
                        return _unitOfWork.UserProfileRepository.Get(filter: u => u.Email.Equals(user)).First().UserName;
                    }
                }

                return "Entrar111";
            }

            //METHOD: Validation of role of User authenticated
            public bool ValUserRole(string roleName)
            {
                var membershipUser = Membership.GetUser(WebSecurity.CurrentUserName);
                if (membershipUser != null)
                {
                    var roles = Roles.GetRolesForUser(membershipUser.UserName);
                    var isSysAdmin = roles.Contains(roleName);

                    return isSysAdmin;
                }

                return false;
            }

            //METHOD: Get EntityId of User authenticated
            public int GetEntityUser()
            {
                if (WebSecurity.IsAuthenticated)
                {
                    var membershipUser = Membership.GetUser(WebSecurity.CurrentUserName);
                    if (membershipUser != null)
                    {
                        //var user = _unitOfWork.UserProfileRepository.Get(f => f.UserName == membershipUser.UserName);
                        var user = _unitOfWork.UserProfileRepository.Get().FirstOrDefault(f => f.Email == membershipUser.UserName);
                        if (user != null) return user.EntityId;
                    }
                }

                return 0;
            }


        }
}